package com.example.database;




import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.util.Log;

public class TaiKhoanTb {
    DatabaseHelp databasehelp;
    public TaiKhoanTb(Context context) {
        databasehelp = new DatabaseHelp(context);
    }
	public final static String TAIKHOAN_TABLE = "TbTaikhoan";
    public final static String TAIKHOAN_MATAIKHOAN = "MaTaiKhoan";
	public final static String TAIKHOAN_USERNAME = "TenTaikhoan";
	public final static String TAIKHOAN_PASS = "Matkhau";
	public final static String TAIKHOAN_HOKHACH = "Ho";
	public final static String TAIKHOAN_TENKHACH = "Ten";
	public final static String TAIKHOAN_TUOI = "Tuoi";
	public final static String TAIKHOAN_GIOITINH = "Gioitinh";
	public final static String TAIKHOAN_DIACHI = "Diachi";
	public final static String TAIKHOAN_SDT = "Sdt";
    public final static String TAIKHOAN_MAIL = "Mail";

	public Cursor fetchAllLopWithQuery(String query) {
		
		Cursor cursor = databasehelp.selectQuery(query);
		if (cursor != null)
			cursor.moveToFirst();
		return cursor;
	}
	public long insertTaiKhoan(String username, String password,String ho,String ten,String tuoi,String gioitinh,String diachi,String phone,String mail) {
		ContentValues values = new ContentValues();
		values.put(TAIKHOAN_USERNAME, username);
		values.put(TAIKHOAN_PASS, password);
		values.put(TAIKHOAN_HOKHACH, ho);
        values.put(TAIKHOAN_TENKHACH, ten);
        values.put(TAIKHOAN_TUOI, tuoi);
        values.put(TAIKHOAN_GIOITINH, gioitinh);
        values.put(TAIKHOAN_DIACHI, diachi);
        values.put(TAIKHOAN_SDT, phone);
        values.put(TAIKHOAN_MAIL, mail);
        long kq = databasehelp.insert_table(TAIKHOAN_TABLE, values);
        if(kq == -1)
        {
            Log.e("SQL", "Loi Insert");
        }
        else
        {
            Log.e("SQL", "Thanh cong");
        }
		return kq;
		
	}
	public Cursor getTaikhoan(String username, String password) {
		String query = "SELECT * FROM TbTaikhoan where "+TAIKHOAN_USERNAME+"='"+ username + "' and "+TAIKHOAN_PASS+"='" + password + "' limit 1";
		Cursor cursor = databasehelp.selectQuery(query);
		if (cursor != null)
		{
			if (cursor.getCount() > 0) {/// co phan tu se tra ve doi tuong cursor
				cursor.moveToFirst();
				return cursor;
			} else {
				// tra ve du lieu null
				return null;
			}
			
		}
		else {
			// tra ve du lieu null
			return null;
		}
			
		
		
		
	}
	
}
